package vn.candyapp.qlct.adapter;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Calendar;
import java.util.Date;
import vn.candyapp.qlct.R;
import vn.candyapp.qlct.database.DBAdapter;
import vn.candyapp.qlct.database.model.ThongKeDS;
import vn.candyapp.qlct.database.model.ThongKeItem;
import vn.candyapp.qlct.item.KieuTKGiaoDich;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ThongKeAdapter extends BaseExpandableListAdapter {

	private Activity activity;
	private ThongKeDS[] mData;
	
	private String[] mGroupTitle = new String[] {
		"TỔNG THU", "TỔNG CHI"
	};
	
	public ThongKeAdapter(Activity activity) {
		this.activity = activity;
	}
	
	public void load(final KieuTKGiaoDich choose, final Runnable run) {
		final DBAdapter dbAdapter = new DBAdapter(activity);
		final Date date = Calendar.getInstance().getTime();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				mData = dbAdapter.getThongKe(date, choose);
				activity.runOnUiThread(run);
			}
		}).start();
	}
	
	@Override
	public int getGroupCount() {
		return mGroupTitle.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mData[groupPosition].getCategories().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mData[groupPosition];
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return ((ThongKeDS) getGroup(groupPosition)).getCategories().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			convertView = LayoutInflater.from(activity).inflate(R.layout.simple_listview_group, parent, false);
		}
		
		TextView groupTitle = (TextView) convertView.findViewById(R.id.tv_group_name);
		groupTitle.setText(mGroupTitle[groupPosition]);
		
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			convertView = LayoutInflater.from(activity).inflate(R.layout.thongke_list_item, parent, false);
		}
		
		TextView tvCatName = (TextView) convertView.findViewById(R.id.tv_category_thongke);
		TextView tvTotal = (TextView) convertView.findViewById(R.id.tv_total_sum);
		
		ThongKeItem item = (ThongKeItem) getChild(groupPosition, childPosition);
		tvCatName.setText(item.getCatName());
		tvTotal.setText(int2CurrencyStr(Float.valueOf(item.getAmount()).intValue()));
		
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	public String int2CurrencyStr(int value) {
		DecimalFormat df = new DecimalFormat();
		DecimalFormatSymbols dfs = new DecimalFormatSymbols();
		dfs.setGroupingSeparator('.');
		df.setGroupingSize(3);
		df.setGroupingUsed(true);
		df.setDecimalFormatSymbols(dfs);
		return df.format(value);

	}

}
